package cn.sinata.wanglin.adapter;

import android.view.View;
import android.widget.TextView;

import com.xilada.xldutils.adapter.HFRecyclerAdapter;
import com.xilada.xldutils.adapter.util.ViewHolder;

import java.util.List;

import cn.sinata.wanglin.R;
import cn.sinata.wanglin.model.Tag;

/**
 *
 * Created by liaoxiang on 16/12/12.
 */

public class GroupTagsAdapter extends HFRecyclerAdapter<Tag> {

    public GroupTagsAdapter(List<Tag> mData) {
        super(mData, R.layout.item_group_tag_layout);
    }

    @Override
    public void onBind(int position, final Tag tag, ViewHolder holder) {
        TextView tv_name = holder.bind(R.id.tv_name);
        tv_name.setText(tag.getName());
        tv_name.setSelected(tag.isSelected());
//        View tv_selected = holder.bind(R.id.tv_selected);
//        tv_selected.setVisibility(tag.isSelected()?View.VISIBLE:View.GONE);
        tv_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean isSelected = view.isSelected();
                if (!isSelected) {
                    if (getSelectedSize() >= 3) {
                        showToast("最多选择3个️标签");
                        return;
                    }
                }
                view.setSelected(!isSelected);
                tag.setSelected(!isSelected);
                notifyDataSetChanged();
            }
        });
    }

    private int getSelectedSize() {
        int count = 0;
        for (Tag tag : mData) {
            if (tag.isSelected()) {
                count++;
            }
        }
        return count;
    }

    public String getSelectTag(){
        StringBuilder builder = new StringBuilder();
        for (Tag tag : mData) {
            if (tag.isSelected()) {
                builder.append(tag.getName());
                builder.append(",");
            }
        }
        if (builder.length() > 0) {
            builder.deleteCharAt(builder.length()-1);
        }
        return builder.toString();
    }
}
